define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mechanism/category/index' + location.search,
                    add_url: 'mechanism/category/add',
                    addzhuanye_url: 'mechanism/category/addzhuanye',
                    addjibie_url: 'mechanism/category/addjibie',

                   // edit_url: 'mechanism/category/edit',
                    editzhuanye_url: 'mechanism/category/editzhuanye',
                    editjibie_url: 'mechanism/category/editjibie',

                    del_url: 'mechanism/category/del',
                    multi_url: 'mechanism/category/multi',
                    import_url: 'mechanism/category/import',
                    table: 'mechanism_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // pk: 'id',
                // sortName: 'weigh',
                // pk: 'weigh',
                // sortName: 'weigh',

                pk: 'id',
                sortName: 'weigh',
                sortOrder: "desc",
                search: false,
                // commonSearch: true,
                showExport: false,
                showToggle: false,
                showSearchButton: false,
                searchFormVisible: false,
                 pagination: false,
                // commonSearch: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),

                            formatter:function (value, row, index) {
                                // console.log(row);
                                if (row.level == '1') {
                                    return '<span style="color:#18bc9c">' + value + '</span>';
                                }else if(row.level ==2){
                                    return '<span style="color:red">' + value + '</span>';
                                }else if(row.level ==3){
                                    return '<span>' + value + '</span>';
                                }
                            }


                        },
                        {
                            field: 'name', title: __('Name'), align: 'left', formatter: function (value, row, index) {

                                if (row.level == '1') {
                                    return '<span style="color:#18bc9c">' + value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;') + '</span>';
                                }else if(row.level ==2){
                                    return '<span style="color:red">' + value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;') + '</span>';
                                }else if(row.level ==3){
                                    return '<span>' + value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;') + '</span>';
                                }

                                //return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }

                },

                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'level', title: __('等级'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [

                                {
                                    name: 'Edit',
                                    text: __('edit'),
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-success',
                                    url:'mechanism/category/edit?ids={id}',
                                    visible:function(row){//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if(row.level==1){
                                            return true;//显示
                                        }else{
                                            return false;//隐藏
                                        }
                                    },

                                    refresh:true
                                },
                                {
                                    name: 'Edit',
                                    text: __('edit'),
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-success',
                                    url:'mechanism/category/editzhuanye?ids={id}',
                                    visible:function(row){//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if(row.level== 2){
                                            return true;//显示
                                        }else{
                                            return false;//隐藏
                                        }
                                    },

                                    refresh:true
                                },
                                {
                                    name: 'Edit',
                                    text: __('edit'),
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-success',
                                    url:'mechanism/category/editjibie?ids={id}',
                                    visible:function(row){//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if(row.level== 3){
                                            return true;//显示
                                        }else{
                                            return false;//隐藏
                                        }
                                    },

                                    refresh:true
                                },


                            ], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },



        add: function () {
            Controller.api.bindevent();
        },
        addzhuanye: function () {
            Controller.api.bindevent();
        },
        addjibie: function () {
            Controller.api.bindevent();
        },

        edit: function () {
            Controller.api.bindevent();
        },

        editzhuanye: function () {
            Controller.api.bindevent();
        },
        editjibie: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

